package com.yang.jxj.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yang.jxj.entity.Students;
import com.yang.jxj.mapper.StudentsMapper;
import com.yang.jxj.service.IStudentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;


import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yang
 * @since 2024-06-21
 */
@Service
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students> implements IStudentsService {



    public void export(HttpServletResponse response) throws Exception {
        List<Students> students = this.list();

        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.addHeaderAlias("studentId","学号");
        writer.addHeaderAlias("name","姓名");
        writer.addHeaderAlias("enrollmentYear","入学年份");
        writer.addHeaderAlias("department","系别");
        writer.addHeaderAlias("grade","年级");
        writer.addHeaderAlias("contactInfo","手机号");
        writer.addHeaderAlias("isDelete","是否真正删除");
        writer.addHeaderAlias("creatTime","创建时间");
        writer.addHeaderAlias("updateTime","修改时间");


        writer.write(students,true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("学生信息","UTF-8");
        response.setHeader("content-disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out,true);
        out.close();
        writer.close();
    }




}
